package com.zwy.stack.impl;

import com.zwy.linear.SLNode;
import com.zwy.stack.Stack;
/*
    链栈
 */
public class StackSLinked implements Stack {
    private SLNode top; //链表首节点引用
    private int size;   //栈的大小

    //初始化数据
    public StackSLinked() {
        top=null;
        size=0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public void push(Object e) {
        SLNode q=new SLNode(e,top);
        top=q;
        size++;
    }

    @Override
    public Object pop() throws Exception {
        if(size<1){
            throw new StackOverflowError("");
        }
        Object obj=top.getData();
        top=top.getNext();
        size--;
        return obj;
    }

    @Override
    public Object peek() throws Exception {
        if(size<1){
            throw new StackOverflowError("");
        }
        return top.getData();
    }
}
